/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package multiples_colas;

// OOSimL v 1.1 File: Server.java, Mon Mar 19 17:11:54 2012
 
import java.util.*; 
import psimjava.*; 
/** 
description
   OOSimL model of the multi-server multi-queue car-wash system
   J Garrido. Updated March 18, 2012.

   This class defines the server objects, which represent
    is the car-wash machines. 
    A server object takes the car at the head of
   the queue and services the car. When the machine completes service
   of the car, it gets another car from the car queue
   If the car queue is empty, it joins the server queue and goes
   into its idle state (suspends itself)
*/
 public  class Server  extends psimjava.Process     {
static Scanner scan = new Scanner (System.in);
 private int  snum; 
  // server number 
 private Customers currentCustomer; 
 // current customer 
 private void  serviceCustomer (   ) { 
 double  startTime; 
  // time of start of service 
 double  service_per; 
  // service period for car  
 String  objname; 
  String  ccustname; 
  double  ccustservt; 
  // cust service time 
 double  simclock; 
  if ( !Multiples_colas.car_queue [snum].empty()) { 
 // get customer from head of waiting queue 
currentCustomer = ( Customers) Multiples_colas.car_queue [snum].out();
 // 
objname =  get_name();
ccustname =  currentCustomer.get_name();
 // get cust service time 
ccustservt =  currentCustomer.get_serv_dur();
 startTime = StaticSync.get_clock();
 // service start time 
System.out.println(objname+ 
" begins service of: "+ 
ccustname+ 
" at: "+ 
startTime+ 
" for "+ 
ccustservt);
 // 
 // 
tracedisplay(objname+ 
" starts service of "+ 
ccustname);
 // accumulate waiting time for this customer (car) 
 // add (startTime - currentCustomer.get_arrivalT())  
 //       to Multiples_colas.custWaitTime 
Multiples_colas.custWaitTime =  (Multiples_colas.custWaitTime) + (( (startTime) - (currentCustomer.get_arrivalT())) );
 // 
service_per =  currentCustomer.get_serv_dur();
  delay(service_per);
 // 
 simclock = StaticSync.get_clock();
 Multiples_colas.custServiceTime += ( (simclock) - (startTime)) ;
System.out.println(objname+ 
" completes service of: "+ 
ccustname+ 
" at: "+ 
simclock);
tracedisplay(objname+ 
" completes service of "+ 
ccustname);
 reactivate(currentCustomer);
 // let car continue   
 Multiples_colas.accum_serv [snum] += service_per;
 }
 else {
return  ; 
 } // endif 
 }  // end serviceCustomer 
 //     
 public Server( String  sname, int  servnum) { 
super(sname); 
snum =  servnum;
System.out.println(sname+ 
" created");
 } // end initializer 
 // 
 public void  Main_body(   ) { 
 double  startIdle; 
  double  idle_period; 
  // idle period 
 double  simclock; 
  // simulation ime 
 String  sname; 
  // server name  
sname =  get_name();
 simclock = StaticSync.get_clock();
System.out.println(sname+ 
" starting main body at "+ 
simclock);
 // 
 while ( simclock < Multiples_colas.simPeriod ) { 
System.out.println(sname+ 
" searching in queue "+ 
snum+ 
" for car");
tracedisplay(sname+ 
" searching in queue "+ 
snum+ 
" for car");
 if ( Multiples_colas.car_queue [snum].empty()) { 
 // car queue is empty 
startIdle =  get_clock();
 // starting time of idle period 
System.out.println(sname+ 
" goes idle at "+ 
simclock);
tracedisplay(sname+ 
" to idle state");
 // suspend server 
 // 
 // reactivated by a car object 
 deactivate(this);
 // queue must now be nonempty           
simclock =  get_clock();
 // set idle_period = simclock - startIdle 
System.out.println(sname+ 
(" reactivated at ") + (simclock));
 } // endif 
serviceCustomer(); 
 // service the car 
 } // endwhile 
System.out.println("terminating "+ 
sname);
tracedisplay("terminating "+ 
sname);
  terminate();
 }  // end Main_body 
 // 
} // end class/interf Server 
